//
//  XTMyLoginViewController.m
//  BeautifulHome
//
//  Created by dlios on 15-5-18.
//  Copyright (c) 2015年 ACE. All rights reserved.
//

#import "XTMyLoginViewController.h"
#import "XTMyLoginColorViewController.h"
#import "XTMyCaseViewController.h"
#import "XTMyStrategyViewController.h"
#import "XTMyGoodsViewController.h"

@interface XTMyLoginViewController ()<UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>
@property (nonatomic, retain) NSMutableArray *mArr;
@property (nonatomic, retain) NSMutableArray *tArr;
@property (nonatomic, retain) UIImageView *imageView;
@end

@implementation XTMyLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    self.imageView.userInteractionEnabled = YES;
    _imageView.image = [UIImage imageNamed:@"999b"];
    [self.view addSubview:_imageView];
    [self.imageView release];
    [self creatNotificationCenter];
    self.mArr = [@[@"我的攻略", @"我的单品", @"我的案例", @"清除缓存", @"一键换肤", @"关于我们"]mutableCopy];
    self.tArr = [@[@"1.png", @"2.png", @"3.png",  @"7.png", @"6.png", @"8.png"]mutableCopy];
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.rowHeight = 80;
    tableView.tag = 1000;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view sendSubviewToBack:_imageView];
    [self.view addSubview:tableView];
    [tableView release];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //通过数据数组动态获取行数
    return _mArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1.设置重用标识
    static NSString *reuseID = @"reuse";
    //2.从重用池中获取一个cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
    //3.判断cell是否存在
    //如果不存在 创建一个新的cell
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseID] autorelease];
    }
    //4.cell设置
    //数组下标和tableView的行数(row)有对应关系
    cell.textLabel.text = _mArr[indexPath.row];
    [cell.textLabel setFont:[UIFont systemFontOfSize:18]];
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.imageView.image = [UIImage imageNamed:self.tArr[indexPath.row]];
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    
    //行列信息字符串
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //控制行高
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 100;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UITableView *tableView1 = (UITableView *)[self.view viewWithTag:1000];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView1.bounds.size.width, 80)];
    view.backgroundColor = [UIColor clearColor];
    return view;
   
    
}
#warning TableView点击响应方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //取消选中阴影效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        XTMyStrategyViewController *mySVC = [[[XTMyStrategyViewController alloc] init] autorelease];
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:mySVC];
        [navi.navigationBar setBackgroundImage:[UIImage imageNamed:@"999"] forBarMetrics:UIBarMetricsDefault];

        [self presentViewController:navi animated:YES completion:^{
            
        }];
    }
    if (indexPath.row == 1) {
        XTMyGoodsViewController *myGoodsVC = [[[XTMyGoodsViewController alloc] init] autorelease];
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:myGoodsVC];
        [navi.navigationBar setBackgroundImage:[UIImage imageNamed:@"999"] forBarMetrics:UIBarMetricsDefault];
        [self presentViewController:navi animated:YES completion:^{
        
        }];
    }
    if (indexPath.row == 3) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"即将清除缓存,是否继续?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"继续", nil];
        [alertView show];
        
    }
    if (indexPath.row == 4) {
        XTMyLoginColorViewController *myColorVC = [[XTMyLoginColorViewController alloc] init];
        [self presentViewController: myColorVC animated:YES completion:^{
            
        }];
    }
    if (indexPath.row == 5) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"美物·俪家V1.0" message:@"感谢您对美物·俪家的使用与关注,如有建议,请联系QQ:1406629129." delegate:self cancelButtonTitle:@"返回" otherButtonTitles:nil, nil];
        [alertView show];
    }
    if ([_mArr[indexPath.row]isEqualToString: @"我的案例"]  ) {
        XTMyCaseViewController *mCase = [[[XTMyCaseViewController alloc]init]autorelease];
        //跳转之前加 Navigation
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:mCase];
        [nav.navigationBar setBackgroundImage:[UIImage imageNamed:@"999"] forBarMetrics:UIBarMetricsDefault];
        [self presentViewController:nav animated:YES completion:^{
            
        }];
    }
}
- (void)creatNotificationCenter
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(changeColor:) name:@"color" object:nil];
}
- (void)changeColor:(NSNotification *)noti
{
    self.navigationController.navigationBar.translucent = NO;
    self.backgroundColor = noti.object;
    NSString *str = [NSString stringWithFormat:@"%@b", self.backgroundColor];
    self.imageView.image = [UIImage imageNamed:str];
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        [fileManager removeItemAtPath:path error:nil];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
